#include "CompositionManager.h"

CompositionManager::CompositionManager() : root (0),
										   compositionLoadedOK (false),
										   compositionSavedOK (false)
{
}

CompositionManager::~CompositionManager()
{
	delete root;
}

bool CompositionManager::loadComposition(const String &pathToCompositionFile)
{
	String basePath = File::getCurrentWorkingDirectory().getFullPathName();
	String toFilePath = basePath + "/res/" + pathToCompositionFile;

	File compositionFile = File(toFilePath);

	XmlDocument doc(compositionFile);
	XmlElement* main = doc.getDocumentElement();

	if(main == 0)
	{
		compositionLoadedOK = false;
	}
	else
	{
		root = new RootElement();
		getRecursiveElement(root, main);
		
		compositionLoadedOK  = true;
	}
	delete main;

	return compositionLoadedOK;
}

bool CompositionManager::saveComposition(ContainerComponent * root)
{
	XmlElement * elements = new XmlElement ("ELEMENT");
	elements->setAttribute(T("type"), T("root"));

	recursiveSaveComposition(root, elements);

	String content = elements->createDocument(String::empty);

	String basePath = File::getCurrentWorkingDirectory().getFullPathName();
	String toFilePath = basePath + "/res/" + "composition.cfg";

	File compositionFile = File(toFilePath);

	compositionFile.create();

	bool ok = compositionFile.replaceWithText(content);

	delete elements;

	return ok;
}

void CompositionManager::recursiveSaveComposition(ContainerComponent * root, XmlElement * element)
{
	for(int i = 0; i < root->getNumElements(); i++)
	{
		XmlElement * elem = new XmlElement(T("ELEMENT"));
		ElementType type = root->getElementAt(i)->getDockElement()->getType();
		String title = root->getElementAt(i)->getDockElement()->getTitle();
		String imgURL = root->getElementAt(i)->getDockElement()->getImgURL();

		switch(type)
		{
			case fileHandler:
				{
				FileProcessorElement * fpe = dynamic_cast<FileProcessorElement*> (root->getElementAt(i)->getDockElement());
				String filePath = fpe->getFilePath();
				elem->setAttribute(T("type"), T("fileHandler"));
				elem->setAttribute(T("imgURL"), imgURL);
				elem->setAttribute(T("title"), title);
				elem->setAttribute(T("filePath"), filePath);
				break;
				}
			case separator:
				elem->setAttribute(T("type"), T("separator"));
				break;
			case container:
				elem->setAttribute(T("type"), T("container"));
				elem->setAttribute(T("imgURL"), imgURL);
				elem->setAttribute(T("title"), title);
				recursiveSaveComposition(root->getElementAt(i)->getChildContainer(), elem);
				break;
		}
		
		element->addChildElement(elem);
	}
}

bool CompositionManager::isValidCompositionLoaded()
{
	return compositionLoadedOK;
}

RootElement * CompositionManager::getRoot()
{
	return root;
}

void CompositionManager::getRecursiveElement(DockElement* elem, XmlElement* main)
{	
	forEachXmlChildElement (*main, e)
	{
		if (e->hasTagName (T("ELEMENT")))
		{
			String type = e->getStringAttribute(T("type"));
			String imageUrl = e->getStringAttribute(T("imgURL"), String::empty);
			String title = e->getStringAttribute(T("title"), String::empty);
			String filePath = e->getStringAttribute(T("filePath"), String::empty);

			DockElement * child = 0;

			if(type == T("container"))
			{	
				child = new ContainerElement(title, imageUrl);
				getRecursiveElement(child, e);
			}
			else
			{
				if(type == T("fileHandler"))
					child = new FileProcessorElement(filePath, title, imageUrl);
				else 
					if(type == T("separator"))
						child = new SeparatorElement();
					else
						if(type == T("container"))
						{
							child = new ContainerElement(title, imageUrl);
						}

			}

			if(child)
				elem->addChild(child);
		}
	}
}


